VB Sample Code for modifying title, options, margins, and date

Private Sub mnuFormatOptions_Click()
Dim result%, jobnum%, mainjob%, TitleHandle&, TitleLength%, RepYear%, RepMonth%, RepDay%
Dim RepLeft%, RepRight%, RepTop%, RepBottom%
Dim ReportTitle$, TempText$
Dim PrintOptions As PEPrintOptions

If Not CRYSTAL_PRO Then
MsgBox "Options are a Crystal Pro only feature.", vbOKOnly + vbCritical, "Crystal Pro Only"
Exit Sub
End If

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If

jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in FormatOptions", jobnum%

' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in FormatOptions", mainjob%
End If

' Load the Options form for filling in the option details of the report
Load Options
CenterForm Sample, Options

' Get the report title
result% = PEGetReportTitle(jobnum%, TitleHandle&, TitleLength%)
ErrorTrap "GetReportTitle in FormatOptions", jobnum%

' Preload the ReportTitle with enough space for the title
ReportTitle$ = String$(TitleLength%, 0)

' Get the title string itself
result% = crvbHandleToBstr(TitleHandle&, ReportTitle$, TitleLength%)
ErrorTrap "HandleToBstr for ReportTitle in FormatOptions", jobnum%

' Load the title onto the form
Options!txtTitle.Text = ReportTitle$

' Get the print date for the report
result% = PEGetPrintDate(jobnum%, RepYear%, RepMonth%, RepDay%)
ErrorTrap "GetPrintDate in FormatOptions", jobnum%

Options!txtDate.Text = Trim$(Str$(RepMonth%)) & "/" & Trim$(Str$(RepDay%)) & "/" & Trim$(Str$(RepYear%))

' Get print options
PrintOptions.StructSize = PE_SIZEOF_PRINT_OPTIONS
result% = PEGetPrintOptions(jobnum%, PrintOptions)
ErrorTrap "GetPrintOptions in FormatOptions", jobnum%

' If page range is set 1 and -1, set options page to All
If PrintOptions.StartPageN = 1 And PrintOptions.stopPageN = -1 Then
Options!txtPageFrom.Enabled = False
Options!txtPageTo.Enabled = False
Options!chkPageAll.Value = 1 ' Checked
Else
Options!txtPageFrom.Enabled = True
Options!txtPageTo.Enabled = True
Options!chkPageAll.Value = 0 ' Unchecked
Options!txtPageFrom.Text = PrintOptions.StartPageN
Options!txtPageTo.Text = PrintOptions.stopPageN
End If
Options!txtCopies.Text = PrintOptions.nReportCopies
' Set collation combo box
Select Case PrintOptions.collation
Case PE_UNCOLLATED
Options!cmbCollation.ListIndex = 1
Case PE_COLLATED
Options!cmbCollation.ListIndex = 2
Case PE_DEFAULTCOLLATION
Options!cmbCollation.ListIndex = 0
End Select

' Get Margins
result% = PEGetMargins(jobnum%, RepLeft%, RepRight%, RepTop%, RepBottom%)
ErrorTrap "GetMargins in FormatOptions", jobnum%

Options!txtLeft.Text = RepLeft%
Options!txtRight.Text = RepRight%
Options!txtTop.Text = RepTop%
Options!txtBottom.Text = RepBottom%

' Show the options form 1 ' Modally
Options.Show 1 ' Modal

' Check what button was pressed
Select Case Options.Tag
Case "Ok"
If MsgBox("Do you wish to modify the report options of this print job?", vbYesNo + vbQuestion, "Change Report Options?") = vbYes Then
' Set the new report title
ReportTitle$ = Trim$(Options!txtTitle.Text) & Chr$(0)
result% = PESetReportTitle(jobnum%, ReportTitle$)
ErrorTrap "SetReportTitle in FormatOptions", jobnum%

' Set print date
RepYear% = Year(DateValue(Options!txtDate.Text))
RepMonth% = Month(DateValue(Options!txtDate.Text))
RepDay% = Day(DateValue(Options!txtDate.Text))
result% = PESetPrintDate(jobnum%, RepYear%, RepMonth%, RepDay%)
ErrorTrap "SetPrintDate in FormatOptions", jobnum%

' Set print options
If Options!chkPageAll.Value = 1 Then
PrintOptions.StartPageN = 1
PrintOptions.stopPageN = -1
Else
PrintOptions.StartPageN = Val(Options!txtPageFrom.Text)
PrintOptions.stopPageN = Val(Options!txtPageTo.Text)
End If
PrintOptions.nReportCopies = Val(Options!txtCopies.Text)
Select Case Options!cmbCollation.Text
Case "Collated"
PrintOptions.collation = PE_COLLATED
Case "Uncollated"
PrintOptions.collation = PE_UNCOLLATED
Case "Default"
PrintOptions.collation = PE_DEFAULTCOLLATION
End Select
result% = PESetPrintOptions(jobnum%, PrintOptions)
ErrorTrap "SetPrintOptions in FormatOptions", jobnum%

' Set Margins
RepLeft% = Val(Options!txtLeft.Text)
RepRight% = Val(Options!txtRight.Text)
RepTop% = Val(Options!txtTop.Text)
RepBottom% = Val(Options!txtBottom.Text)
result% = PESetMargins(jobnum%, RepLeft%, RepRight%, RepTop%, RepBottom%)
ErrorTrap "SetMargins in FormatOptions", jobnum%
End If
Case "Cancel"
MsgBox "Cancel was pressed - no changes will be made to the print options", vbOKOnly + vbCritical, "Cancel Pressed"
End Select
Unload Options

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-link preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Report Options Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in FormatOptions", jobnum%

result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in FormatOptions", jobnum%

result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If

' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in FormatOptions", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Options Complete!", vbOKOnly, "Operation Succeeded"

End Sub
ActiveX
Private Sub mnuFormatOptions_Click()
Dim hwndPreviewWindow As Long

If Not CRYSTAL_PRO Then
MsgBox "Options are a Crystal Pro only feature.", vbOKOnly + vbCritical, "Crystal Pro Only"
Exit Sub
End If

CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form

' Load the Options form for filling in the option details of the report
Load Options
CenterForm Sample, Options

' Load the title onto the form
Options!txtTitle.Text = CrystalReport1.WindowTitle

' Get the print date for the report
Options!txtDate.Text = Trim$(Str$(CrystalReport1.PrintMonth)) & "/" & Trim$(Str$(CrystalReport1.PrintDay)) & "/" & Trim$(Str$(CrystalReport1.PrintYear))

' Get print options
' If page range is set 1 and -1, set options page to All
If CrystalReport1.PrinterStartPage <= 1 And CrystalReport1.PrinterStopPage = -1 Then
Options!txtPageFrom.Enabled = False
Options!txtPageTo.Enabled = False
Options!chkPageAll.Value = 1 ' Checked
Else
Options!txtPageFrom.Enabled = True
Options!txtPageTo.Enabled = True
Options!chkPageAll.Value = 0 ' Unchecked
Options!txtPageFrom.Text = CrystalReport1.PrinterStartPage
Options!txtPageTo.Text = CrystalReport1.PrinterStopPage
End If
Options!txtCopies.Text = CrystalReport1.PrinterCopies
' Set collation combo box
Select Case CrystalReport1.PrinterCollation
Case 0 ' Uncollated
Options!cmbCollation.ListIndex = 1
Case 1 ' Collated
Options!cmbCollation.ListIndex = 2
Case 2 ' Default
Options!cmbCollation.ListIndex = 0
End Select

' Get Margins
Options!txtLeft.Text = CrystalReport1.MarginLeft
Options!txtRight.Text = CrystalReport1.MarginRight
Options!txtTop.Text = CrystalReport1.MarginTop
Options!txtBottom.Text = CrystalReport1.MarginBottom

' Show the options form modally
Options.Show 1

' Check what button was pressed
Select Case Options.Tag
Case "Ok"
If MsgBox("Do you wish to modify the report options of this print job?", vbYesNo + vbQuestion, "Change Report Options?") = vbYes Then
' Set the new report title
CrystalReport1.WindowTitle = Trim$(Options!txtTitle.Text)

' Set print date if a valid date
If IsDate(Options!txtDate.Text) Then
CrystalReport1.PrintYear = Year(DateValue(Options!txtDate.Text))
CrystalReport1.PrintMonth = Month(DateValue(Options!txtDate.Text))
CrystalReport1.PrintDay = Day(DateValue(Options!txtDate.Text))
End If

' Set print options
If Options!chkPageAll.Value = 1 Then
CrystalReport1.PrinterStartPage = 1
CrystalReport1.PrinterStopPage = -1
Else
CrystalReport1.PrinterStartPage = Val(Options!txtPageFrom.Text)
CrystalReport1.PrinterStopPage = Val(Options!txtPageTo.Text)
End If
CrystalReport1.PrinterCopies = Val(Options!txtCopies.Text)
Select Case Options!cmbCollation.Text
Case "Collated"
CrystalReport1.PrinterCollation = 0
Case "Uncollated"
CrystalReport1.PrinterCollation = 1
Case "Default"
CrystalReport1.PrinterCollation = 2
End Select

' Set Margins
CrystalReport1.MarginLeft = Val(Options!txtLeft.Text)
CrystalReport1.MarginRight = Val(Options!txtRight.Text)
CrystalReport1.MarginTop = Val(Options!txtTop.Text)
CrystalReport1.MarginBottom = Val(Options!txtBottom.Text)
End If
Case "Cancel"
MsgBox "Cancel was pressed - no changes will be made to the print options", vbOKOnly + vbCritical, "Cancel Pressed"
End Select
Unload Options

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "FormatOptions"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If

' Close the report
CrystalReport1.ReportFileName = ""

MsgBox "Options Complete!", vbOKOnly, "Operation Completed"

End Sub


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com